﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp_PrismEventBus.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow(IEventAggregator eventAggregator)
        {
            //订阅消息

            eventAggregator.GetEvent<EventBus>().Subscribe(Receive);//订阅消息，无参数

            eventAggregator.GetEvent<EventBusArgs>().Subscribe(ReceiveArgs);//订阅消息，有参数

            eventAggregator.GetEvent<EventBusAction>().Subscribe(ReceiveArgs);//订阅消息，带委托

            eventAggregator.GetEvent<EventBusAction>().Subscribe(ReceiveArgsFilter, _data => //订阅消息，带过滤器
            {
                if (_data.UserID < 10)
                    return true;
                return false;
            });

            //通过运行线程控制订阅消息
            //ThreadOption.UIThread 主线程运行
            //ThreadOption.BackgroundThread 在后台线程执行
            //ThreadOption.PublisherThread 消息在哪里发布就再哪里执行
            eventAggregator.GetEvent<EventBusArgs>().Subscribe(ReceiveRunEniv, ThreadOption.PublisherThread);



            //发布消息

            eventAggregator.GetEvent<EventBus>().Publish();//发布无参数消息

            eventAggregator.GetEvent<EventBusArgs>().Publish("123");//发布有参数消息

            eventAggregator.GetEvent<EventBusAction>().Publish(new LoginData() //发布消息，带委托
            {
                UserName = "张三",
                Password = "AAA",
                UserID = 5,
                LoginAction = args => 
                {
                    
                }
            });

            //eventAggregator.GetEvent<EventBusArgs>().Publish("123",)



            InitializeComponent();
        }

        private void Receive()
        { 
            
        }

        private void ReceiveArgs(string args) 
        {
            
        }

        private void ReceiveArgs(LoginData loginData) 
        {
            loginData.LoginAction?.Invoke($"{loginData.UserName}的密码是{loginData.Password}");
        }

        private void ReceiveArgsFilter(LoginData loginData)
        {
            loginData.LoginAction?.Invoke($"{loginData.UserName}的密码是{loginData.Password}");
        }

        private void ReceiveRunEniv(string args)
        {
            int id = Thread.CurrentThread.ManagedThreadId;
        }
    }
}
